Is there a better of writing const data = variableName[0]? I mean for variableName[0]. I do understand data is placed in index 0, but writing a number does not look professional ( to me atleast), So if there is some professional way someone knows, please share
To be more accurate, this is the case when the array has ONLY 1 element
You can try to use the array destructuring syntax:
const variableName = [1];
const data = variableName[0];
console.log(data);
const [data2] = variableName;
console.log(data2);
In above example it takes first array element and ignores the other ones. You can also destructure more than one element into named variables:
const [variable1, variable2] = yourArray;
The following is the shortest syntax in the modern JavaScript:
const [data] = variableName;
I would say that what looks potentially unprofessional is not so much writing a number, but rather storing data in an array like this. If your variable was an object {data: "someData"} instead of an array ["someData"] , then you could write
const {data} = variableName;
If you can't change the format, then there is nothing really wrong with variableName[0], but if you really want to avoid writing a number you could write
const [data] = variableName;
This may look a bit more cryptic to people not used to this syntax, though.